<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../../css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="../../css/app.min.css" />
		<style>
			.mui-row.mui-fullscreen>[class*="mui-col-"] {
				height: 100%;
			}
			
			.mui-col-xs-3,
			.mui-col-xs-9 {
				overflow-y: auto;
				height: 100%;
			}
			
			.mui-segmented-control .mui-control-item {
				line-height: 30px;
				font-size: 80%;
				width: 100%;
				border: 0!important;
			}
			
			.mui-control-content {
				display: block;
			}
			
			.mui-segmented-control.mui-segmented-control-inverted .mui-control-item.mui-active {
				background-color: #fff;
			}
			
			#segmentedControls{
				border-right: hidden!important;
				background-color: white;
			}
			
			.mui-table-view-cell{
				font-size: 80%;
				padding: 6px 15px!important;
				border-bottom: hidden!important;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" id="header">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">分类选择</h1>
		</header>
		<div class="mui-content mui-row mui-fullscreen">
			<div class="mui-col-xs-3">
				<div id="segmentedControls" class="mui-segmented-control mui-segmented-control-inverted mui-segmented-control-vertical">
				</div>
			</div>
			<div id="segmentedControlContents" class="mui-col-xs-9" style="border-left: 1px solid #F7F7F7;">
			</div>
		</div>
		<script src="../../js/mui.min.js"></script>
		<script src="../../js/immersed.js"></script>
		<script>
			mui.init({
				swipeBack: true //启用右滑关闭功能
			});
			mui.plusReady(function() {
				starbar();
				plus.navigator.setStatusBarStyle('dark');
								
				var controls = document.getElementById("segmentedControls");
				var contents = document.getElementById("segmentedControlContents");
				var titlecont = [{item: ['手机','苹果','小米','华为','oppo','vivo','三星','魅族','诺基亚','锤子','一加','htc','美图','努比亚','乐视','联想','酷派','中兴','摩托罗拉','LG','金立','黑莓','索尼','其他手机品牌']},
								 {item: ['图书','文学小说','童书教育','人文社科','科普教育','生活艺术','教材教辅','外文原版书','唱片影片','其他图书']},
								 {item: ['电脑','笔记本','平板','台式机','电脑配件','外设产品','网络设备']},
								 {item: ['数码','摄影器材','耳机','mp3/mp4','游戏机及配件','游戏软件','音响/音响','移动电源','智能穿戴','其他智能设备','kindle/电子书','存储设备','其他数码']},
								 {item: ['服装鞋帽','女装上衣','女装裤子','女装裙子','女装套装','男装上衣','男装裤子','男装套装','女鞋','男鞋','女包','男包','行李箱','衣帽配饰','运动鞋服','家居服/内衣','特色服装','其他服装']},
								 {item: ['交通工具','摩托车','电动车','自行车','平衡车/体感车','摩托车/电动车配件','自行车配件','汽车用品','汽车配件','其他代步工具']},
								 {item: ['母婴用品','婴幼服饰','童车童床','玩具图书','尿裤湿巾','安全座椅','喂养用具','孕妈用品','洗护用品','其他母婴用品']},
								 {item: ['家用电器','电视机','洗衣机','冰箱','空调','热水器','厨房电器','生活小家电','其他家电']},
								 {item: ['家居家具','沙发','床/床垫','柜子','桌子/茶几','椅子/凳子','家装软饰','家纺','灯具照明','厨房卫浴','衣架/架子','其他家居用品']},
								 {item: ['办公用品','打印机/复印机','投影仪','文具','其他办公用品']},
								 {item: ['玩具乐器','玩具','吉他','钢琴','电子琴','古筝','架子鼓','小提琴','二胡','其他乐器']},
								 {item: ['车载设备','导航仪','行车记录仪','倒车雷达','车载音箱','车载冰箱','车载吸尘器','车载香水','储物箱','维修保养设备','其他车载设备']},
								 {item: ['票务卡卷','美食卷','咖啡卷','冰淇凌卷','话费卡','加油卡','电商购物卷','超市/购物卡','健身卡','景点门票','赛事演出','美容美发卡','蛋糕/甜点卡','洗衣卡','电影票','其他卡卷']},
								 {item: ['珠宝配饰','男表','女表','珠宝','其他珠宝食品']},
								 {item: ['宠物用品','狗狗用','猫猫用','花鸟鱼虫用']},
								 {item: ['运动户外','健身训练','户外装备','桌椅棋盘','轮滑','垂钓用品','游泳用品','舞蹈瑜伽','其他运动用品']},
								 {item: ['艺术文玩','邮币票证','文玩','珠宝玉器','书法绘画','其他艺术文玩']},
								 {item: ['美妆个护','护肤','彩妆','口腔护理','洗发护发','香水','美容工具','其他美容用品']},
								 {item: ['技能服务','家政护理','教育培训','it服务','语言翻译','占卜','买房装修','宠物美容','运动健身','其他技能服务']},
								 {item: ['其他分类','求购','保健用品','其他闲置二手']}
								]
				var html = [];
				for(var i = 0; i < titlecont.length; i++) {
					html.push('<a class="mui-control-item" data-index="' + i + '" href="#content' + i + '">' + titlecont[i].item[0] + '</a>');
				}
				controls.innerHTML = html.join('');
				var htmll = [];
				for(var g = 0; g < titlecont.length; g++) {
					htmll.push('<div id="content' + g + '" class="mui-control-content"><ul class="mui-table-view">');
					for(var j = 1; j < titlecont[g].item.length; j++) {
						htmll.push('<li class="mui-table-view-cell">' + titlecont[g].item[j] + '</li>');
					}
					htmll.push('</ul></div>');
				}
				contents.innerHTML = htmll.join('');
				//默认选中第一个
				controls.querySelector('.mui-control-item').classList.add('mui-active');
				//contents.querySelector('.mui-control-content').classList.add('mui-active');
				var controlsElem = document.getElementById("segmentedControls");
				var contentsElem = document.getElementById("segmentedControlContents");
				var controlListElem = controlsElem.querySelectorAll('.mui-control-item');
				var contentListElem = contentsElem.querySelectorAll('.mui-control-content');
				var controlWrapperElem = controlsElem.parentNode;
				var controlWrapperHeight = controlWrapperElem.offsetHeight;
				var controlMaxScroll = controlWrapperElem.scrollHeight - controlWrapperHeight; //左侧类别最大可滚动高度
				var maxScroll = contentsElem.scrollHeight - contentsElem.offsetHeight; //右侧内容最大可滚动高度
				var controlHeight = controlListElem[0].offsetHeight; //左侧类别每一项的高度
				var controlTops = []; //存储control的scrollTop值
				var contentTops = [0]; //存储content的scrollTop值
				var length = contentListElem.length;
				for(var i = 0; i < length; i++) {
					controlTops.push(controlListElem[i].offsetTop + controlHeight);
				}
				for(var i = 1; i < length; i++) {
					var offsetTop = contentListElem[i].offsetTop;
					if(offsetTop + 100 >= maxScroll) {
						var height = Math.max(offsetTop + 100 - maxScroll, 100);
						var totalHeight = 0;
						var heights = [];
						for(var j = i; j < length; j++) {
							var offsetHeight = contentListElem[j].offsetHeight;
							totalHeight += offsetHeight;
							heights.push(totalHeight);
						}
						for(var m = 0, len = heights.length; m < len; m++) {
							contentTops.push(parseInt(maxScroll - (height - heights[m] / totalHeight * height)));
						}
						break;
					} else {
						contentTops.push(parseInt(offsetTop));
					}
				}
				contentsElem.addEventListener('scroll', function() {
					var scrollTop = contentsElem.scrollTop;
					for(var i = 0; i < length; i++) {
						var offsetTop = contentTops[i];
						var offset = Math.abs(offsetTop - scrollTop);
						//						console.log("i:"+i+",scrollTop:"+scrollTop+",offsetTop:"+offsetTop+",offset:"+offset);
						if(scrollTop < offsetTop) {
							if(scrollTop >= maxScroll) {
								onScroll(length - 1);
							} else {
								onScroll(i - 1);
							}
							break;
						} else if(offset < 20) {
							onScroll(i);
							break;
						} else if(scrollTop >= maxScroll) {
							onScroll(length - 1);
							break;
						}
					}
				});
				var lastIndex = 0;
				//监听content滚动
				var onScroll = function(index) {
					if(lastIndex !== index) {
						lastIndex = index;
						var lastActiveElem = controlsElem.querySelector('.mui-active');
						lastActiveElem && (lastActiveElem.classList.remove('mui-active'));
						var currentElem = controlsElem.querySelector('.mui-control-item:nth-child(' + (index + 1) + ')');
						currentElem.classList.add('mui-active');
						//简单处理左侧分类滚动，要么滚动到底，要么滚动到顶
						var controlScrollTop = controlWrapperElem.scrollTop;
						if(controlScrollTop + controlWrapperHeight < controlTops[index]) {
							controlWrapperElem.scrollTop = controlMaxScroll;
						} else if(controlScrollTop > controlTops[index] - controlHeight) {
							controlWrapperElem.scrollTop = 0;
						}
					}
				};
				//滚动到指定content
				var scrollTo = function(index) {
					contentsElem.scrollTop = contentTops[index];
				};
				mui(controlsElem).on('tap', '.mui-control-item', function(e) {
					scrollTo(this.getAttribute('data-index'));
					return false;
				});
				
				mui('#segmentedControlContents').on('tap', '.mui-table-view-cell', function(e) {
					var licont = this.textContent;
					for(var i = 0 ; i < titlecont.length ; i ++){
						for(var j = 1 ; j < titlecont[i].item.length ; j ++){
							if(titlecont[i].item[j] == licont){
								var preView = plus.webview.currentWebview().opener(); //触发上一个页面刷新图片事件
			                    mui.fire(preView,'goods',{ 
			                         goodstype:  titlecont[i].item[0],
			                         goodstypeitem: licont
			                    }); 
			                    mui.back();
							}
						}
					}
//					
				});
			})
		</script>

	</body>

</html>